﻿using FluentValidation;
using Infrastructure.WebApi;
using Neoit.Utils;
using Quartz;

namespace Application.DTO
{
    #region 查询
    public class JobRequest : PageRequest
    {
        public string Keyword { get; set; }
        public string Group { get; set; }
    }
    public class JobResponse
    {
        public int Id { get; set; }
        public DateTime CreateTime { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public string JobType { get; set; }
        public string Cron { get; set; }
        public string NoticeObjects { get; set; }
        public string Creator { get; set; }
        public bool IsEnable { get; set; }
        public DateTime? LastExecutionTime { get; set; }
        public string LastExecutionTimeDes { get; set; }
        public DateTime? NextExecutionTime { get; set; }
        public string NextExecutionTimeDes { get; set; }
        public bool? LastExecutionStatus { get; set; }
        public string Expected { get; set; }
        public string Note { get; set; }
        public string Group { get; set; }
        public string FailureRetryPolicy { get; set; }
        public int RandomDelay { get; set; }

        public string RequestMethond { get; set; }
        public string RequestHeader { get; set; }
        public string RequestBody { get; set; }
        public int LogRetentionDays { get; set; }
        public string Result { get; set; }
        public string RecentExecutionStatus { get; set; }
        public int ExecutionCount { get; set; }
        public NoticePolicy NoticePolicy { get; set; }
        public string LastFailureTime { get; set; }
        /// <summary>
        /// 是否被调度
        /// </summary>
        public bool IsScheduled { get; set; }
    }
    #endregion

    #region 新增、修改
    public class JobFormRequest
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public string Cron { get; set; }
        public JobType JobType { get; set; }
        public string Expected { get; set; }
        public string NoticeObjects { get; set; }
        public string Note { get; set; }
        public string FailureRetryPolicy { get; set; }
        public int? RandomDelay { get; set; }
        public string RequestMethond { get; set; } = "Get";
        public string RequestHeader { get; set; }
        public string RequestBody { get; set; }
        public string Group { get; set; }
        public int LogRetentionDays { get; set; } = 30;
        public NoticePolicy NoticePolicy { get; set; }
    }

    public class JobFormRequestValidator : AbstractValidator<JobFormRequest>
    {
        public JobFormRequestValidator()
        {
            RuleFor(x => x.Title).NotEmpty().WithName("标题");
            RuleFor(x => x.Cron).NotEmpty().WithName("Cron表达式").DependentRules(() =>
            {
                RuleFor(s => s.Cron).Must(s =>
                {
                    try
                    {
                        new CronExpression(s);
                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                }).WithMessage("无效的Cron表达式。");
            });

            When(x => x.JobType == JobType.Url, () =>
            {
                RuleFor(x => x.Content).NotEmpty().WithName("Url").DependentRules(() =>
                {
                    RuleFor(x => x.Content).Matches("^(https?|ftp):\\/\\/.*").WithMessage("Url应以http、https或ftp开头");
                });
            });
            When(x => x.JobType == JobType.Script, () =>
            {
                RuleFor(x => x.Content).NotEmpty().WithName("Script");
            });

            RuleFor(s => s.RandomDelay).Must(s => s < (60 * 30)).WithMessage("随机延迟时间不能超过30分钟。").When(s => s.RandomDelay!=null);

            RuleFor(s => s.FailureRetryPolicy).Must(s =>
            {
                var failureRetryPolicys = s.Replace("，", ",").Split(",").Select(s => s.Trim()).Where(s => s.Has());
                if (!failureRetryPolicys.All(a => a.IsNaturalNumber())) return false;
                s = failureRetryPolicys.JoinString(",");
                return true;
            }).WithMessage("失败重试策略的值，应为使用逗号“,”分隔的数值字符串(单位：秒)；如10,60,180。").When(s => s.FailureRetryPolicy.Has());
        }
    }
    #endregion

    #region 记录
    public class JobRecordsRequest : PageRequest
    {
        public int JobId { get; set; }
        public bool onlyFailed { get; set; } = false;

    }
    #endregion

}
